package com.xinqi.modules.databank.v2.controller.resource.web;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.modules.databank.dto.req.*;
import com.xinqi.modules.databank.dto.rsp.*;
import com.xinqi.modules.databank.dto.rsp.question.QuestionAnalysisResultDTO;
import com.xinqi.modules.databank.dto.rsp.question.UserQuestionResultDTO;
import com.xinqi.modules.databank.v2.domain.CourseResourcePartEntity;
import com.xinqi.modules.databank.v2.service.analysis.CourseResourceAnalysisService;
import com.xinqi.modules.databank.v2.service.answer.ResourceInteractionLogService;
import com.xinqi.modules.databank.v2.service.part.ResourcePartService;
import com.xinqi.modules.databank.v2.service.question.ResourceQuestionService;
import com.xinqi.modules.databank.v2.service.resource.CourseResourceDataService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资料WEB接口-V2
 *
 * @author: system
 * @date: 2023/03/14
 */
@Tag(name = "资料WEB接口-V2", description = "资料web")
@RestController
@RequestMapping("/web/resource/data")
@RequiredArgsConstructor
@Validated
public class ResourceDataWebController {

    private final CourseResourceDataService resourceDataService;

    private final ResourcePartService resourcePartService;

    private final ResourceInteractionLogService resourceInteractionLogService;

    private final ResourceQuestionService resourceQuestionService;

    private final CourseResourceAnalysisService resourceAnalysisService;

     /**
      * 添加资料
      * @Author
      * @Date 13:51 2023/3/22
      * @Description
      * @Return
      * @Exception
      */
    @PostMapping
    public R<String> insert(@Validated @RequestBody CourseResourceDataCreateDTO dto){
        resourceDataService.addResource(dto);
        return R.ok(MessageUtils.message("common.release.success"));
    }
    /**
     * 修改资料
     * @Author
     * @Date 13:51 2023/3/22
     * @Description
     * @Return
     * @Exception
     */
    @PutMapping("/{id}")
    public R<String> update(@PathVariable("id") Long id, @Validated @RequestBody CourseResourceDataUpdateDTO dto){
        resourceDataService.updateResource(dto);
        return R.ok(MessageUtils.message("common.update.success"));
    }

    /**
     * 发布资料
     * @Author 星星
     * @Date 15:02 2023/3/22
     * @param: 课件id(路径参数)
     * @Return
     * @Exception
     */
    @GetMapping("/release/{id}")
    public R<String> releaseInteractiveCourse(@PathVariable("id") Long id){
        if (resourceDataService.releaseResourceData(id)) {
            return R.ok(MessageUtils.message("course.release.message.success"));
        } else {
            return R.fail(MessageUtils.message("course.release.message.fail"));
        }
    }

    /**
     * 发布资料-V2版本
     * @Author 星星
     * @Date 15:02 2023/3/22
     * @param: 课件id(路径参数)
     * @Return
     * @Exception
     */
    @GetMapping("/release/{id}/v2")
    public R<String> releaseInteractiveCourseV2(@PathVariable("id") Long id){
        if (resourceDataService.releaseResourceDataV2(id)) {
            return R.ok(MessageUtils.message("course.release.message.success"));
        } else {
            return R.fail(MessageUtils.message("course.release.message.fail"));
        }
    }

    /**
     * 下载资料(批量下载和单个下载公用)
     * @Author 星星
     * @Date 15:02 2023/3/17
     * @param: id集合
     * @Return
     * @Exception
     */
    @PostMapping("/donwloads")
    public R<List<String>> donwloadsResource(@RequestBody DownloadDTO downloadDto){
        List<String> urls=resourceDataService.downResourceData(downloadDto);
        return R.ok(urls);
    }

    /**
     * 根据id查询资料详情
     * @Author dzy
     * @Return
     */
    @GetMapping("/getInfo/{contentId}")
    public R<ResourceResponseDTO> getResourceInfo(@PathVariable("contentId") Long contentId){
        ResourceResponseDTO info = resourceDataService.getResourceDataInfo(contentId);
        return R.ok(info);
    }

    /**
     * 获取资料-互动环节-列表
     * @Author dzy
     * @Return
     */
    @GetMapping("/part/list")
    public R<List<CourseResourcePartEntity>> getParts(CourseResourcePartQueryDTO query) {
        List<CourseResourcePartEntity> list = resourcePartService.selectPart(query);
        return R.ok(list);
    }

    /**
     * 获取资料文件-试题-详情
     * @Author dzy
     * @Return
     */
    @GetMapping("/file/question")
    public R<UserQuestionResultDTO> getFileInfo(CourseResourcePartQueryDTO query) {
        UserQuestionResultDTO result = resourcePartService.selectPartByResourceId(query);
        return R.ok(result);
    }

    /**
     * 获取资料文件-试题-答题
     * @Author dzy
     * @Return
     */
    @PostMapping("/file/answer")
    public R<String> fileAnswer(@RequestBody ResourceInteractionLogCreateDTO create) {
        if (resourceInteractionLogService.answerQuestion(create)) {
            return R.ok("答题成功");
        } else {
            return R.fail("答题失败");
        }
    }

    /**
     * 资料文件-提问
     * @Author dzy
     * @Return
     */
    @PostMapping("/file/question/add")
    public R<QuizResultDTO> fileQuestionAdd(@RequestBody CourseResourceQuestionCreateDTO query) {
        QuizResultDTO result = resourceQuestionService.addQuiz(query);
        return R.ok(result);
    }

    /**
     * 资料文件-提问-回复
     * @Author dzy
     * @Return
     */
    @PostMapping("/file/question/reply")
    public R<QuizResultDTO> fileQuestionReply(@RequestBody CourseResourceQuestionCreateDTO query) {
        QuizResultDTO result = resourceQuestionService.replyQuiz(query);
        return R.ok(result);
    }

    /**
     * 资料文件-提问-删除
     * @Author dzy
     * @Return
     */
    @DeleteMapping("/file/question/remove/{id}")
    public R<String> fileQuestionRemove(@PathVariable("id") Long id) {
        resourceQuestionService.removeQuiz(id);
        return R.ok();
    }

    /**
     * 资料文件-提问-点赞
     * @Author dzy
     * @Return
     */
    @DeleteMapping("/file/question/give/{id}")
    public R<String> fileQuestionGive(@PathVariable("id") Long id) {
        resourceQuestionService.give(id);
        return R.ok();
    }

    /**
     * 资料文件-提问-列表
     * @Author dzy
     * @Return
     */
    @GetMapping("/file/question/page")
    public R<PageInfo<QuizResultDTO>> fileQuestionPage(CourseResourceQuestionQueryDTO query) {
        PageInfo<QuizResultDTO> result = resourceQuestionService.selectQuiz(query);
        return R.ok(result);
    }

    /**
     * 资料文件-提问-子列表
     * @Author dzy
     * @Return
     */
    @GetMapping("/file/question/son/page")
    public R<PageInfo<QuizResultDTO>> fileQuestionSonPage(CourseResourceQuestionQueryDTO query) {
        PageInfo<QuizResultDTO> result = resourceQuestionService.selectSonQuiz(query);
        return R.ok(result);
    }

    /**
     * 资料学习统计
     * @Author 星星
     * @Date 15:02 2023/3/22
     * @Description
     * @Return
     * @Exception
     */
    @GetMapping("/resourceStatistics")
    public R<List<ResourceStatisticsDTO>> resourceStatistics(@Validated ResourceQueryDTO resourceQueryDTO){
        List<ResourceStatisticsDTO> infos = resourceDataService.resourceStatistics(resourceQueryDTO);
        return R.ok(infos);
    }

    /**
     * 更新试题分析
     * @Author dzy
     */
    @GetMapping("/refresh/question/analysis/{contentId}")
    public R<String> refreshQuestionAnalysis(@PathVariable("contentId") Long contentId){
        resourceAnalysisService.refreshQuestionAnalysis(contentId);
        return R.ok("更新成功");
    }

    /**
     * 获取试题分析数据
     * @Author dzy
     */
    @GetMapping("/find/question/analysis")
    public R<ResourceAnalysisResultDTO> findQuestionAnalysis(@Validated ResourceAnalysisQueryDTO query){
        return R.ok(resourceAnalysisService.findQuestionAnalysis(query));
    }

    /**
     * 获取每个文件或互动环节试题分析 详情数据
     * @Author dzy
     */
    @GetMapping("/find/interaction/question/analysis/detail")
    public R<List<QuestionAnalysisResultDTO>> findInteractionQuestionAnalysisDetail(@Validated ResourceQuestionAnalysisQueryDTO query){
        return R.ok(resourceAnalysisService.questionAnalysisList(query));
    }

    /**
     * 获取试题分析 详情数据 正确/错误人员
     * @Author dzy
     */
    @GetMapping("/find/question/analysis/user")
    public R<List<ResourceStudentLogUserResultDTO>> findQuestionAnalysis(@Validated ResourceQuestionAnalysisQueryDTO query){
        return R.ok(resourceAnalysisService.answerDetails(query));
    }

}
